package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	import gs.events.TweenEvent;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	public class FrameSubtitle extends JFrame
	{
		//members define
		private var labelContent:MultilineLabel;
		public function FrameSubtitle()
		{
			setClosable(false);
			setResizable(false);  
			setDragable(false);
			setBorder(new EmptyBorder());
			this.setTitleBar(null);
			this.setBackgroundDecorator(null);
			
			//component creation
			setSize(new IntDimension(1200, 600));
			setPreferredSize(new IntDimension(1200, 600));
			var layout0:CenterLayout = new CenterLayout();
			getContentPane().setLayout(layout0);
			
			labelContent = new MultilineLabel();
			labelContent.setSize(new IntDimension(100, 4));
			labelContent.setEditable(false);
			labelContent.setWordWrap(true);
			
			//component layoution
			getContentPane().append(labelContent);
			
			
			setBackgroundDecorator(new SolidBackground(new ASColor(0x000000,0.9)));
			
			labelContent.setEditable(false);
			labelContent.setWordWrap(true);
			labelContent.setBackgroundDecorator(null);
			labelContent.setPreferredWidth(600);
			
			addEventListener(MouseEvent.CLICK, onClick);
			
			labelContent.setFont(new ASFont("宋体", 32, true, false, false, false));
			//labelContent.filters = new Array(new GlowFilter(ASColor.BLACK.getARGB(), 1, 2, 2, 10));
			labelContent.setForeground(ASColor.ORANGE);
			

			this.setClipMasked(false);
		}
		
		private function onClick(e:Event):void
		{
			if(isLeaving)
				return;
			
			if(!_enter)
				return;
			if(_contextCount < _strContent.length)
			{
				if(_timerMove != null)
					_timerMove.stop();
				_contextCount = _strContent.length;
				labelContent.setHtmlText(_strContent);
				return;
			}
			Client.getInstance().sendPacketLogic(903,[int(_nNPCDialogNpcId),int(_nIndex)]);
			onLeave();
		}
		
		private var _enter:Boolean = false;
		private function onEnter():void
		{
			if(_tweenLeave != null)
			{
				_tweenLeave.clear();
				_tweenLeave = null;
			}
			
			if(_enter)
				return;

			isLeaving = false;
			labelContent.setHtmlText("");
			/*x = 0;
			y = -600;
			TweenLite.to(this,0.5,{x:0, y:0,onComplete:function():void
			{
				//PanelMainHelp.getInstance().onEnterTalk();
				setData();
				_enter = true;
			}});*/
			x = 0;
			y = 0;
			this.alpha = 0.2;
			TweenLite.to(this,0.5,{autoAlpha:1.0,onComplete:function():void
			{
				//PanelMainHelp.getInstance().onEnterTalk();
				setData();
				_enter = true;
			}});
			
			//_tween = TweenLite.to(this, 0.5, {autoAlpha:1.0});
			
			//PanelMainHelp.getInstance().onEnterTalk();
			_enter = true;
		}
		private var isLeaving:Boolean = false;
		private var _tweenLeave:TweenLite = null;
		private function onLeave():void
		{
			if(!_enter)
				return;
			if(_tweenLeave != null)
			{
				_tweenLeave.clear();
				_tweenLeave = null;
			}
			/*x = 0;
			y = 0;
			_tweenLeave = TweenLite.to(this,0.5,{x:0, y:-600,onComplete:function():void
			{
				leave();
			}});*/
			
			x = 0;
			y = 0;
			this.alpha = 1.0;
			isLeaving = true;
			_tweenLeave = TweenLite.to(this,0.5,{autoAlpha:0.2,onComplete:function():void
			{
				leave();
				isLeaving = false;
			}});
			
			//_enter = false;
			
			
		}
		
		public function leave():void
		{
			if(!_enter)
				return;
			FrameSubtitle.hide();
			if(_tweenLeave != null)
			{
				_tweenLeave.clear();
				_tweenLeave = null;
			}
			_enter = false;
		}
		public static var _frame:FrameSubtitle = null;
		public static var _nNPCDialogNpcId:int = 0;
		public static var _strContent:String = "";
		public static var _nIndex:int = 0;
		public static function show():void
		{
			//if(isShowing())
			//{
			//_frame.setData();
			//	return;
			//}
			if(isShowing())
				_frame.leave();
			if(_frame == null)
			{
				_frame = new FrameSubtitle();
				Client.getInstance().mainWindow.addChild(_frame);
			}
			_frame.show();
			//PanelMainHelp.getInstance().onEnterTalk();
			Client.getInstance().mainWindow.bringToTop(_frame);
			_frame.onEnter();
			//_frame.setData();
			
		}
		public static function hide():void
		{
			//if(_frame._mcNPCImage != null && _frame.containsChild(_frame._mcNPCImage))
			//{
			//	_frame.removeChild(_frame._mcNPCImage);
			//	_frame._mcNPCImage = null;
			//}
			if(isShowing())
			{
				_frame.labelContent.setHtmlText("");
				_frame.hide();
				//PanelMainHelp.getInstance().onLeaveTalk();
			}
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
		
		public static function phSubtitle_903(e:PacketEvent):void
		{
			
			var msg:Array = e.data as Array;
			trace("phSubtitle_903", msg);
			var nIndex:uint = 0;
			_nNPCDialogNpcId = msg[nIndex++];
			_strContent = msg[nIndex++];
			_nIndex = msg[nIndex++];

			show();
		}
		
		private function onTimerMove(e:TimerEvent):void
		{

			_contextCount++;
			
			if(_contextCount > _strContent.length)
			{
				_timerMove.stop();
				return;
			}
			labelContent.setHtmlText(_strContent.substr(0,_contextCount));
		}
		
		private var _timerMove:Timer = null;
		private var _contextCount:int = 1;
		public function setData():void
		{
			_contextCount = 1;
			//if(_mcNPCImage != null && this.containsChild(_mcNPCImage))
			//{
			//	removeChild(_mcNPCImage);
			//	_mcNPCImage = null;
			//}
			/*var mc:MovieClip = _currentCharNpc._mc_actions[Actions.Stand];
			if(mc == null)
			{
			hide();
			return;
			}
			//var imgSelf:JPanel = panelImage;
			mc.gotoAndStop(1);
			mc.scaleX = 2;
			mc.scaleY = 2;
			mc.x = panelContent.x - mc.width;//(width - mc.width)/2;//+mc.width/2;
			mc.y = height - mc.height;
			//imgSelf.setBackgroundDecorator(null);*/
			
			/*_mcNPCImage = _currentCharNpc.imageBitmap;
			if(_mcNPCImage != null)
			{
			_mcNPCImage.scaleX = 2;
			_mcNPCImage.scaleY = 2;
			_mcNPCImage.x = panelContent.x - _mcNPCImage.width;//(width - mc.width)/2;//+mc.width/2;
			_mcNPCImage.y = height - _mcNPCImage.height;
			addChild(_mcNPCImage);
			//_mcNPCImage.cacheAsBitmap = true;
			}*/
			
			//txtTalkSelf.setHtmlText('<font color=\"#d9941b\">'+_currentCharNpc.getName()+':</font>'+_strNPCDialogAsk);
			if(_strContent.length == 0)
				return;
			labelContent.setHtmlText(_strContent.substr(0,_contextCount));

			if(_timerMove == null)
			{
				_timerMove = new Timer(100);
				_timerMove.addEventListener(TimerEvent.TIMER,onTimerMove);
			}
			if(!_timerMove.running)
				_timerMove.start();
			
			//txtTalkSelf.pack();
			getContentPane().pack();
			labelContent.pack();
			this.pack();
			this.paintImmediately();
		}
	}
}